def plotspec(x, Ts):
fig = figure()
ax1 = fig.add_subplot(211)
ax1.plot(x)
q = fft.fft(x)
ax2 = fig.add_subplot(212)
ax2.plot(fft.fftfreq(len(x), Ts), abs(q))
def arbspec(s, time, Ts):
t = linspace(0.0, time, time/Ts)
x = s(t)
plotspec(x, Ts)
def speccos(f, phi, time, Ts):
def s(t):
return cos(2*pi*f*t+phi)
arbspec(s, time, Ts)
3.5. Mimic the code in speccos.m to find the spectrum of a cosine wave
for f in [1,2,20,30]:
speccos(f, 0, 2.0, 1.0/100.0)
for phi in [0,0.1,pi/8.0,pi/2.0]:
speccos(10, phi, 2.0, 1.0/100.0)
For different sampling rates, Ts=1/10,1/1k, 1/10k
for Fs in [10,1000,10000]:
speccos(10, 0, 2.0, 1.0/float(Fs))
3.6. Let x1(t) be a cosine wave of frequency f = 10, x2(t) be of f=18 and x3 be of f=33. Let x(t) = x1+0.5x2+2x3. Find the spectrum of x. What property of the FT does this illustrate?
def cosf(f, b):
def s(t):
return b*cos(2*pi*f*t)
return s
def xt(t):
x1 = cosf(10, 1.0)
x2 = cosf(18, 0.5)
x3 = cosf(33, 2.0)
return x1(t) + x2(t) + x3(t)
arbspec(xt, 2.0, 1.0/100.0)
3.7. Find the spectrum of a cosine wave when:
def osccos(phit, ft):
def s(t):
return cos(2.0*pi*ft(t)*t + phit(t))
return s
def ocspec(phit, ft):
arbspec(osccos(phit, ft), 2.0, 1.0/100.0)
ocspec(lambda t: 10*pi*t, lambda t: linspace(10,10,len(t)))
ocspec(lambda t: pi*(t**2), lambda t: linspace(10,10,len(t)))
ocspec(lambda t: linspace(0,0,len(t)), lambda t: sin(2*pi*t))
ocspec(lambda t: linspace(0,0,len(t)), lambda t: t**2)